﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Exhibition.Common.Helper;
using Exhibition.Dao.ViewModel;
using Exhibition.Service;

namespace Exhibition.Manage
{
    public partial class CompUserDetail : BaseAdminPage
    {
        UserModel model = new UserModel();
        UserCompModel pmodel = new UserCompModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hfId.Value = reqID.ToString();
                GetUser();
                GetPrope(5,ltrCountry,"rdogj");
                GetPrope1(4,  ltrnature, "rdonature");
                GetFw();
            }
        }

        private void GetUser()
        {
            model = CenterService.userService.GetOneById(reqID);
            if (model != null) {
                txtUserName.Value = model.UserName;
                txtRealName.Value = model.RealName;
                txtCompName.Value = model.CompName;
                txtDepart.Value = model.Depart;
                txtPosition.Value = model.Position;
                hfLevel.Value = model.UserLevel.ToString();
                txtMobile.Value = model.Mobile;
                txtEmail.Value = model.Email;

                if (model.compMdl != null)
                {
                    txtCompAddr.Value = model.compMdl.CompAddr;
                    txtPostCode.Value = model.compMdl.PostCode;
                    txtPhoneStart.Value = model.compMdl.PhoneStart;
                    txtPhoneNum.Value = model.compMdl.PhoneNum;
                    txtFaxStart.Value = model.compMdl.FaxStart;
                    txtFaxNum.Value = model.compMdl.FaxNum;
                    txtCompSite.Value = model.compMdl.CompSite;
                    
                    txtRegYear.Value = model.compMdl.RegYear.ToString() ;
                    txtRegMoney.Value = model.compMdl.RegMoney.ToString();
                    txtUserNum.Value = model.compMdl.UserNum.ToString();
                    txtLincense.Value = model.compMdl.Lincense;
                    otherCountry.Value = model.compMdl.OtherCountry;


                }

            }
        }
        /// <summary>
        /// 获取参展范围
        /// </summary>
        private void GetFw()
        {
            StringBuilder sb = new StringBuilder();
            List<ProductClassModel> pds = CenterService.productClassService.GetListByPid(CurrentSite.Id);
            foreach (var c in pds)
            {
                string strchk = string.Empty;
                if (pmodel != null)
                {
                    if (!string.IsNullOrEmpty(pmodel.ProductIds))
                    {
                        if (model.Properties.Contains(c.Id.ToString()))
                        {
                            strchk = " checked='checked'";
                        }
                    }
                }

                sb.AppendFormat("<input type='checkbox' name='chkPClass' value='{0}' {2}/>{1}&nbsp;", c.Id, c.ClassName, strchk);
            }
            ltrFW.Text = sb.ToString();
        }
        
        private void GetPrope(int tpid,Literal ltr,string controlname)
        {
            StringBuilder sb = new StringBuilder();
            List<CodesModel> cdms = CenterService.codesService.GetModels(tpid); 
            foreach (var c in cdms)
            {
                string strchk = string.Empty;
                if (model != null)
                {
                    if (!string.IsNullOrEmpty(model.Properties))
                    {
                        if (model.Properties.Contains(c.ECode))
                        {
                            strchk = " checked='checked'";
                        }
                    }
                }

                sb.AppendFormat("<input type='radio' name='{3}' value='{0}' {2}/>{1}&nbsp;", c.ECode, c.EName, strchk,controlname);
            }
            ltr.Text = sb.ToString();
        }

        private void GetPrope1(int tpid, Literal ltr, string controlname)
        {
            StringBuilder sb = new StringBuilder();
            List<CodesModel> cdms = CenterService.codesService.GetModels(tpid);
            foreach (var c in cdms)
            {
                string strchk = string.Empty;
                if (model != null)
                {
                    if (model.compMdl != null)
                    {
                        if (!string.IsNullOrEmpty(model.compMdl.Compnature))
                        {
                            if (model.compMdl.Compnature.Contains(c.ECode))
                            {
                                strchk = " checked='checked'";
                            }
                        }
                    }
                }

                sb.AppendFormat("<input type='radio' name='{3}' value='{0}' {2}/>{1}&nbsp;", c.ECode, c.EName, strchk, controlname);
            }
            ltr.Text = sb.ToString();
        }



        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (model == null) {
                model = new UserModel();
            }
            model.Id = Convert.ToInt32(hfId.Value);
            model.UserName = txtUserName.Value;
            model.PassWord = string.IsNullOrEmpty(txtPassWord.Value) ? model.PassWord : StringUtils.EncryptPassWord(txtPassWord.Value,StringUtils.PassFormat.MD5);
            model.RealName = txtRealName.Value;
            model.CompName = txtCompName.Value;
           
            model.Depart = txtDepart.Value;
            model.Position = txtPosition.Value;
            //model.Nationality=
            model.UserAddress = txtCompAddr.Value;
            model.Email = txtEmail.Value;
            model.Sex = 1;
            model.Mobile = txtMobile.Value;
            model.AreaId = 0;
            model.AreaPid = 0;
            model.Nationality = Request["rdogj"];
            model.Properties = Request["rdonature"];
            model.IsDelete = 0;
            model.IsMember = 0;
            model.IsCheck = 0;

            //是否审核
            //model.IsCheck=
            model.UserType = 1;
            model.UserLevel =Convert.ToInt32(  hfLevel.Value);
            if (hfId.Value == "0" || string.IsNullOrEmpty(hfId.Value))
            {
                model.SiteID =CurrentSite.Id ;
                model.CreateTime = DateTime.Now;
                model.CreateUserId = AdminUser.Id;
            }


             if (model.compMdl == null) {
                model.compMdl = new UserCompModel();
            }

            model.compMdl.CompName = txtCompName.Value;

            model.compMdl.CompLogo = "";
            model.compMdl.PostCode = txtPostCode.Value; 
            model.compMdl.Contact = txtRealName.Value;
            model.compMdl.CountryCode = txtCountryCode.Value;
            model.compMdl.PhoneStart = txtPhoneStart.Value;
            model.compMdl.PhoneNum = txtPhoneNum.Value;
            model.compMdl.FaxStart = txtFaxStart.Value;
            model.compMdl.FaxNum = txtFaxNum.Value;
            if (model.Id > 0)
            {
                model.compMdl.UserId = model.Id;
            }
            model.compMdl.IsDelete = 0;
            if (model.Id == 0) {
                model.compMdl.SiteID = CurrentSite.Id;
            }
            model.compMdl.ProductIds = Request["chkPClass"];
            model.compMdl.OtherCountry = otherCountry.Value;
            model.compMdl.CompLevel =Convert.ToInt32( hfLevel.Value);
            model.compMdl.CompSite = txtCompSite.Value;
            model.compMdl.CompAddr = txtCompAddr.Value;
            model.compMdl.RegMoney =string.IsNullOrEmpty(txtRegMoney.Value)?0: Convert.ToInt32(txtRegMoney.Value);
            model.compMdl.RegYear = string.IsNullOrEmpty(txtRegYear.Value) ? DateTime.Now.Year : Convert.ToInt32(txtRegYear.Value);
            model.compMdl.UserNum = string.IsNullOrEmpty(txtUserNum.Value) ? 0 : Convert.ToInt32(txtUserNum.Value);
            model.compMdl.Lincense = txtLincense.Value;
            model.compMdl.Compnature = Request["rdonature"];
           
            if (CenterService.userService.Save(model) > 0) {

                JsHelper.Unblock(this, CommFunMsg.GetMessage("A0001", Lan.zh));
            }
            else
            {
                JsHelper.Unblock(this, CommFunMsg.GetMessage("e0001", Lan.zh));
            }

        
        }

    }
}